local zhuishe = fk.CreateSkill{
  name = "hanqing__zhuishe",
}

Fk:loadTranslationTable{
  ["hanqing__zhuishe"] = "追慑",
  [":hanqing__zhuishe"] = "轮次开始时，你可以选择一名手牌数不等于你的角色，本轮前两次你或其手牌数变为与对方相等后，你可以摸至多两张牌，"..
  "其可以弃置至多两张牌，第三次时，你对其造成1点伤害，然后你可以令其下回合开始时发动〖迫决〗。",

  ["#hanqing__zhuishe-choose"] = "追慑：选择一名角色，本轮双方手牌数变为相等后执行效果",
  ["@hanqing__zhuishe-round"] = "追慑",
  ["#hanqing__zhuishe-draw"] = "追慑：你可以摸至多两张牌",
  ["#hanqing__zhuishe-discard"] = "追慑：你可以弃至多两张牌",
  ["#hanqing__zhuishe-invoke"] = "追慑：是否令 %dest 下回合开始时发动“迫决”？",
}

zhuishe:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(zhuishe.name) and
      table.find(player.room.alive_players, function (p)
        return p:getHandcardNum() ~= player:getHandcardNum()
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p:getHandcardNum() ~= player:getHandcardNum()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = zhuishe.name,
      prompt = "#hanqing__zhuishe-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(player, "hanqing__zhuishe-round", to.id)
    room:setPlayerMark(player, "@hanqing__zhuishe-round", {to.general, 0})
  end,
})

zhuishe:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if player:getMark("hanqing__zhuishe-round") ~= 0 then
      local to = player.room:getPlayerById(player:getMark("hanqing__zhuishe-round"))
      if player:getHandcardNum() ~= to:getHandcardNum() or player.dead or to.dead then return end
      for _, move in ipairs(data) do
        if move.from == player or move.from == to then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
        if (move.to == player or move.to == to) and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, {tos = {player.room:getPlayerById(player:getMark("hanqing__zhuishe-round"))}})
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(player:getMark("hanqing__zhuishe-round"))
    local n = player:getMark("@hanqing__zhuishe-round")[2] + 1
    if n == 3 then
      room:setPlayerMark(player, "hanqing__zhuishe-round", 0)
      room:setPlayerMark(player, "@hanqing__zhuishe-round", 0)
      room:damage{
        from = player,
        to = to,
        damage = 1,
        skillName = zhuishe.name,
      }
      if not player.dead and not to.dead and
        room:askToSkillInvoke(player, {
          skill_name = zhuishe.name,
          prompt = "#hanqing__zhuishe-invoke::"..to.id,
        }) then
        room:setPlayerMark(to, "hanqing__pojue_invoke", 1)
      end
    else
      room:setPlayerMark(player, "@hanqing__zhuishe-round", {to.general, n})
      n = room:askToNumber(player, {
        skill_name = zhuishe.name,
        prompt = "#hanqing__zhuishe-draw",
        min = 1,
        max = 2,
        cancelable = true,
      })
      if n then
        player:drawCards(n, zhuishe.name)
        if to.dead then return end
      end
      if to:isNude() then return end
      room:askToDiscard(to, {
        min_num = 1,
        max_num = 2,
        include_equip = true,
        skill_name = zhuishe.name,
        cancelable = true,
        prompt = "#hanqing__zhuishe-discard",
      })
    end
  end,
})

return zhuishe
